unit dbmanager;

{$mode delphi}

interface

uses
  Classes, SysUtils, Math, SdfData, DB, Forms, DateUtils,
  constants, config;

type
  { TDBManager }

  TDBManager = class(TObject)
  public
    Dataset: TSdfDataset;
    // Methods
    constructor Create;
    destructor Destroy; override;
    function GetDatabasePath: string;
    procedure AddTestResult(VA: Cardinal);
  end;

var
  gDBManager: TDBManager;

implementation

constructor TDBManager.Create;
var
  AField: TField;
begin
  inherited Create;

  Dataset := TSdfDataset.Create(nil);
  Dataset.FieldDefs.Add('USUARIO', ftString);
  Dataset.FieldDefs.Add('ACUIDADE', ftString);
  Dataset.FieldDefs.Add('DATA', ftString);
  Dataset.Filename := GetDatabasePath();
  Dataset.Delimiter := ',';
  Dataset.FirstLineAsSchema := True;

  Dataset.Active := True;
end;

destructor TDBManager.Destroy;
begin
  Dataset.Free;

  inherited Destroy;
end;

function TDBManager.GetDatabasePath: string;
begin
  {$ifdef Windows}
    Result := ExtractFilePath(Application.ExeName) + 'historico.dat';
  {$else}
    //???
  {$endif}
end;

procedure TDBManager.AddTestResult(VA: Cardinal);
begin
  Dataset.Last;
  Dataset.Append;
  Dataset.FieldByName('USUARIO').AsString := gTestConfig.UsuarioAtual;
  Dataset.FieldByName('ACUIDADE').AsString := Format('20 / %d', [VA]);
  Dataset.FieldByName('DATA').AsString := DateToStr(Today) + ' ' + TimeToStr(Now);
  Dataset.Post;
end;

initialization

  gDBManager := TDBManager.Create;

finalization

  gDBManager.Free;

end.

